package com.astrocsr.security;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;

import com.astrocsr.business.AuditLogManager;
import com.astrocsr.common.Constants;
import com.astrocsr.common.helper.ContextHelper;

public class AuthenticationSuccessHandler extends
	SimpleUrlAuthenticationSuccessHandler {

	@Autowired
	private AuditLogManager auditLogManager; 

	public AuthenticationSuccessHandler() {
	}

	public void onAuthenticationSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication) {
		try {
			System.out.println("Catch Login Successfully");
			//setDefaultTargetUrl(defaultTargetUrl); // route login process
			
			// insert log
			auditLogManager.addAuditlog(
					Constants.LOG_ACCT_TYPE_ACCESS,	//0
					Constants.LOG_AcccessCode_Login, 	//1 
					Constants.LOG_AcccessName_Login, 	//2
					ContextHelper.getLoginId(), 		//3
					ContextHelper.getCurrentRole(), 	//4
					"Login", //5
					request.getRemoteAddr()				//6
					);
			
			super.onAuthenticationSuccess(request, response, authentication);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
